package com.cx.controller;

import com.cx.pojo.*;
import com.cx.rabbit.Producer;
import com.cx.service.OrderService;
import com.cx.service.SeckillGoodsOrder;
import com.cx.service.SeckillGoodsService;
import com.cx.service.SeckillVOService;
import com.cx.util.RequestResult;
import com.cx.vo.SeckillGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author 11597
 * @Date 2020/5/26 23:27
 */
@Controller
public class SeckillController {

    @Autowired
    private SeckillVOService seckillVOService;

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private SeckillGoodsOrder seckillGoodsOrder;



    @Autowired
    Producer producer;

    @RequestMapping("/showSeckillGoods")
    public String showSeckillGoods(ServletRequest request,HttpSession session){
        List<SeckillGoodsVO> seckillGoodsVOList = seckillVOService.selectAllSeckillGoods();
        for (int i = 0; i < seckillGoodsVOList.size(); i++) {
            long nowTime = new Date().getTime();
            long endTime = seckillGoodsVOList.get(i).getEndTime().getTime();
            if(endTime<nowTime){
                seckillGoodsVOList.remove(i);
                i--;
            }
        }
        request.setAttribute("seckillGoodsVOList", seckillGoodsVOList);
        return "seckil";
    }

    @RequestMapping("/selectSeckillGoodsById")
    public ModelAndView selectByIdSeckillGoods(Integer goodId){
        System.out.println("查询商品:goodId->"+ goodId);
        ModelAndView modelAndView = new ModelAndView();
        SeckillGoodsVO seckillGoodsVO = seckillVOService.selectByIdSeckillGoods(goodId);
        long nowTime = new Date().getTime();
        System.out.println(nowTime);
        long endTime = seckillGoodsVO.getEndTime().getTime();
        System.out.println(endTime);
        int  countdown = (int) (endTime - nowTime)/1000;
        System.out.println(countdown);
        modelAndView.setViewName("seckillGoods");
        modelAndView.addObject("seckillGoodsVO",seckillGoodsVO);
        modelAndView.addObject("countdown",countdown);
        return modelAndView;
    }


    @RequestMapping("/seckillGoods")
    @ResponseBody
    public Map<Object,Object> seckillGoods(HttpSession session, Integer goodId){
        Map<Object,Object> map = new HashMap<>();
        System.out.println(goodId);
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            map.put("code",0);
            return map;
        }
        RequestResult requestResult = new RequestResult();
        SeckillGoods seckillGoods = seckillGoodsService.selectSeckillGoodsByGoodId(goodId);

        long nowTime = new Date().getTime();
        long endTime = seckillGoods.getEndTime().getTime();
        int  countdown = (int) (endTime - nowTime)/1000;
        System.out.println(countdown);
        System.out.println(seckillGoods.getSeckillNum());
        if(seckillGoods.getSeckillStock()>0&&countdown>0){
            int orderId = seckillGoodsOrder.addSeckillGoodsOrder(goodId,user.getId(),seckillGoods);
            map.put("orderId",orderId);
            map.put("message","success");
            map.put("code",1);
        }else{
            map.put("message","fail");
            map.put("code",2);
        }
        return map;
    }



    @RequestMapping("/seckillOrder")
    public ModelAndView seckillOrder(HttpSession session, Integer orderId,Integer goodId){
        System.out.println(orderId);
        System.out.println(goodId);
        ModelAndView modelAndView = new ModelAndView();
        Users user = (Users) session.getAttribute("user");
        SeckillGoodsVO seckillGoodsVO = seckillVOService.selectByIdSeckillGoods(goodId);
        List<Address> addressList = orderService.selectByUserId(user.getId());
        modelAndView.addObject("seckillGoodsVO",seckillGoodsVO);
        modelAndView.addObject("addressList",addressList);
        modelAndView.addObject("orderId",orderId);
        modelAndView.setViewName("orderDetail");
        return modelAndView;
    }





}
